package hammersmith.local.ui.gui;

import hammersmith.local.valueobjects.Article;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class StockChartPanel extends JPanel {

	private static final long serialVersionUID = 8787849245811515043L;

	ChartPanel cp;

	public StockChartPanel(Article a, int[] changes) {

		XYSeries series = new XYSeries("Bestandskurve");

		int days = changes.length-1;
		int currentVal = 0;
		
		for (int i = days; i >= 0; i--) {
			series.add(i, changes[currentVal]);
			currentVal++;
		}

		// Fügt dem Dataset die Serie zu!
		XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(series);
		// Generiert den Graphen
		JFreeChart chart = ChartFactory.createXYLineChart(a.getName(), // Titel
																		// des
																		// Graphen
				"Tage", // Beschriftung der X-Achse
				"Bestand", // Beschriftung der Y-Achse
				dataset, // Dataset
				PlotOrientation.VERTICAL, // Orientierung
				true, // Legende zeigen
				true, // Tooltips zeigen
				false);

		// Achsennummierung
		NumberAxis xAxis = new NumberAxis();
		xAxis.setTickUnit(new NumberTickUnit(1));

		// Gibs dem Chart!
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setDomainAxis(xAxis);
		cp = new ChartPanel(chart);

	}

	public JPanel getPanelView() {
		JPanel pnl = new JPanel();
		pnl.add(cp);
		return pnl;
	}

}
